<template>
  <!-- 营销推广 ==》 需设置权限可见 ==》 主针对热门赛事 热门活动 今日推荐 公司账号或内部人员可见 -->
  <div class="pageWrap">
    <el-button type="primary" @click="addbanner()">添加</el-button>
    <el-table :data="BannerData" row-key="id" class="homeBanner">
      <el-table-column prop="date" label="图片" width="250">
        <template slot-scope="{row}">
          <video :src="row.imgUrl" v-if="row.mold==1" class="bannerImgS" controls="true"></video>
          <img :src="row.imgUrl" alt class="bannerImgS" v-else />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="300"></el-table-column>
      <el-table-column label="是否显示" width="80">
        <template slot-scope="{row}">
          <span>{{row.state==1?'显示中':'未显示'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="creatTime" label="创建时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{row,$index}">
          <el-button type="primary" icon="el-icon-top" circle @click="toTop($index)"></el-button>
          <el-button type="primary" icon="el-icon-edit" circle @click="editBanner(row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="delBanner(row,$index)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="赛事banner设置"
      :visible.sync="dialogFormVisible"
      :show-close="false"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      v-loading="dialogLoading"
      width="90%"
    >
      <el-form :model="bannerForm" label-width="100px">
        <el-form-item label="类型：">
          <el-radio-group v-model="bannerForm.radio" @change="radioChange">
            <el-radio-button label="1">赛事链接</el-radio-button>
            <el-radio-button label="4">活动链接</el-radio-button>
            <el-radio-button label="5">竞猜链接</el-radio-button>
            <el-radio-button label="2">商城链接</el-radio-button>
            <el-radio-button label="3">其他小程序</el-radio-button>
            <el-radio-button label="6">普通图片</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="选择：" v-if="bannerForm.radio!=3&&bannerForm.radio!=6">
          <el-select
            v-model="bannerForm.nameId"
            filterable
            placeholder="请选择"
            style="width:80%"
            @change="mOsChange"
          >
            <el-option
              v-for="item in matchORshopArr"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称：" v-else>
          <el-input v-model="bannerForm.name" placeholder="请输入内容" clearable style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="图片/视频：" required>
          <el-upload
            ref="upbanner"
            class="avatar-uploader"
            :action="upLoadUrl"
            :show-file-list="false"
            :on-success="(value,file)=> picSuccess(value,file)"
            :before-upload="(file)=> beforeUpload(file)"
            name="img"
            v-loading="ploading"
          >
            <template v-if="bannerForm.imgUrl">
              <video
                v-if="bannerForm.mold==1"
                :src="bannerForm.imgUrl"
                class="avatar banner"
                controls="true"
              ></video>
              <img v-else :src="bannerForm.imgUrl" class="avatar banner" />
            </template>
            <i v-else class="el-icon-plus avatar-uploader-icon banner"></i>
          </el-upload>
          <span class="tips">注：请上传jpg/mp4格式，图片建议尺寸400*247 长方形</span>
        </el-form-item>

        <el-form-item
          label="链接："
          v-if="bannerForm.radio!=1&&bannerForm.radio!=4&&bannerForm.radio!=5&&bannerForm.radio!=6"
        >
          <el-input v-model="bannerForm.links" placeholder="请输入内容" clearable style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="APPID：" v-if="bannerForm.radio==3" required>
          <el-input v-model="bannerForm.appId" placeholder="请输入内容" clearable style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="是否显示：">
          <el-switch
            v-model="bannerForm.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveBanner">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
var matchArr = []; //所有赛事
var shopArr = []; // 所有商品
var activeArr = []; // 所有活动
var guessArr = []; // 所有竞猜
// import Sortable from "sortablejs";
export default {
  data() {
    return {
      ploading: false,
      BannerData: [],
      bannerForm: {
        orgId: "",
        name: "",
        nameId: "",
        imgUrl: "",
        links: "",
        appId: "",
        state: 1, //是否显示
        radio: "1", //赛事==1 商城==2 其他小程序==3  活动==4  竞猜==5  普通图片==6  类型的链接
        type: "4", // 小程序 首页banner==1 赛事页banner==2 活动页banner==3 具体赛事==4
        mold: 0, // 类型 图片 或者 视频 视频为1 默认图片
        seq: 0, // 排序
      },
      matchORshopArr: matchArr,
      dialogFormVisible: false,
      upLoadUrl: this.$url + "/sbr/match/uploadImg",
      dialogLoading: false,
    };
  },
  methods: {
    delBanner(v, index) {
      this.$axios.get("/sbr/slideshow/delSlideShow?id=" + v.id).then((res) => {
        this.BannerData.splice(index, 1);
      });
    },
    toTop(i) {
      if (i == 0) return false;
      let vv = this.BannerData.splice(i, 1)[0];
      this.BannerData.splice(i - 1, 0, vv);
      let ids = this.BannerData.map((v) => {
        return v.id;
      });
      this.$axios
        .post("/sbr/slideshow/orderSlideShow", { ids: ids })
        .then((res) => {
          this.getBannerArr();
        });
    },
    addbanner() {
      this.bannerForm = {
        orgId: "",
        name: "",
        nameId: "",
        imgUrl: "",
        links: "",
        appId: "",
        state: 1, //是否显示
        radio: "1", //赛事==1 商城==2 小程序==3 什么类型的链接
        type: 4, // 小程序 首页banner==1 赛事页banner==2 活动页banner==3
        mold: 0, // 类型 图片 或者 视频 视频为1 默认图片
        seq: 0, // 排序
        matchId: this.$Base64.decode(sessionStorage.getItem("MandA")),
      };
      this.dialogFormVisible = true;
    },
    picSuccess(v, file) {
      console.log(file.raw.type);
      this.bannerForm.imgUrl = v.data.filePath;
      if (file.raw.type == "video/mp4") this.bannerForm.mold = 1;
      else this.bannerForm.mold = 0;
      this.ploading = false;
    },
    beforeUpload(file) {
      this.ploading = true;
      let isJPG = file.type === "image/jpeg";
      let isLt1M = file.size / 1024 / 1024 < 1;
      let isMP4 = file.type === "video/mp4";
      let is30 = file.size / 1024 / 1024 < 30;
      if (isJPG && !isLt1M) {
        this.$message.error("上传图片大小不能超过 1MB!");
        this.ploading = false;
      }
      // if (!isJPG) {
      //   this.$message.error("上传图片只能是 JPG 格式!");
      // }
      if (isMP4 && !is30) {
        this.$message.error("上传视频大小不能超过 30MB!");
        this.ploading = false;
      }
      if (!isJPG && !isMP4) {
        this.$message.error("上传图片只能是 JPG 格式,视频只是MP4格式!");
        this.ploading = false;
      }
      // return isJPG && isLt1M;
      return (isJPG && isLt1M) || (isMP4 && is30);
    },
    // banner 链接类型切换
    radioChange() {
      this.bannerForm.name = "";
      this.bannerForm.nameId = "";
      this.bannerForm.imgUrl = "";
      this.bannerForm.links = "";
      this.bannerForm.appId = "";
      if (this.bannerForm.radio == 1) this.matchORshopArr = matchArr;
      else if (this.bannerForm.radio == 2) this.matchORshopArr = shopArr;
      else if (this.bannerForm.radio == 4) this.matchORshopArr = activeArr;
      else if (this.bannerForm.radio == 5) this.matchORshopArr = guessArr;
    },
    // banner 获取赛事与商品
    getMorS() {
      this.$axios.get("/sbr/match/getMatchBanner").then((res) => {
        let rr = res.data.data;
        activeArr = rr.filter((v) => {
          return v.type == "H";
        });
        matchArr = rr.filter((v) => {
          return v.type == "S";
        });
        if (this.bannerForm.radio == 1) this.matchORshopArr = matchArr;
        if (this.bannerForm.radio == 4) this.matchORshopArr = activeArr;
        this.$axios.get("/sbr/shop/getShopKuPic").then((res) => {
          let rr = res.data.data;
          shopArr = rr;
          if (this.bannerForm.radio == 2) this.matchORshopArr = shopArr;
          this.$axios.get("/sbr/match/guess/getGuessList").then((res) => {
            let rr = res.data.data;
            rr = rr.map((v) => {
              return { name: v.matchName, id: v.matchId };
            });
            guessArr = rr;
            if (this.bannerForm.radio == 5) this.matchORshopArr = guessArr;
          });
        });
      });
    },
    // 选择赛事或商品
    mOsChange() {
      let val = "";
      if (this.bannerForm.radio == 1) {
        val = matchArr.find((v) => {
          return v.id == this.bannerForm.nameId;
        });
      } else if (this.bannerForm.radio == 4) {
        val = activeArr.find((v) => {
          return v.id == this.bannerForm.nameId;
        });
      } else if (this.bannerForm.radio == 2) {
        val = shopArr.find((v) => {
          return v.id == this.bannerForm.nameId;
        });
      }
      if (val) {
        this.bannerForm.name = val.name;
        this.bannerForm.imgUrl = val.banner;
        //赛事==1 商城==2 其他小程序==3  活动==4  竞猜==5  普通图片==6  类型的链接
        if (this.bannerForm.radio == 1) {
          this.bannerForm.links = "/pages/match/detail/detail?m=" + val.id;
        } else if (this.bannerForm.radio == 4) {
          this.bannerForm.links = "/active/detail/detail?a=" + val.id;
        } else if (this.bannerForm.radio == 5) {
          this.bannerForm.links = "/pages/guess/gussA/gussA?m=" + val.id;
        } else {
          if (val.links) this.bannerForm.links = val.links;
        }
      }
    },
    // 修改banner
    editBanner(row) {
      // editBannerCon=row
      row.nameId = row.name;
      this.bannerForm = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    saveBanner() {
      if (!this.bannerForm.imgUrl) {
        this.$message.error("请上传图片");
        return false;
      }
      if (this.bannerForm.radio == 3 && !this.bannerForm.appId) {
        this.$message.error("其他小程序请填写appId");
        return false;
      }
      this.dialogLoading = true;
      this.bannerForm.orgId = this.$Base64.encode(this.$orgId);
      this.$axios
        .post("/sbr/slideshow/putSlideShow", this.bannerForm)
        .then((res) => {
          this.dialogLoading = false;
          this.dialogFormVisible = false;
            this.getBannerArr();
        });
    },
    // 查询banner
    getBannerArr() {
      let oo = {
        type: 4,
        matchId: this.$Base64.decode(sessionStorage.getItem("MandA")),
      };
      this.$axios
        .get("/sbr/slideshow/getSlideShowList", { params: oo })
        .then((res) => {
          let rr = res.data.data;
          this.BannerData = rr;
        });
    },
  },
  mounted() {
    this.getMorS();
    this.getBannerArr();
  },
};
</script>
<style scoped>
/* @import url(); 引入公共css类 */
.marketNav {
  width: 100%;
  height: 100%;
  position: absolute;
}
.marketNav >>> .el-tabs__nav-scroll {
  background-color: #545c64;
}
.marketNav >>> .el-tabs__item {
  color: #fff;
}
.marketNav >>> .el-tabs__item.is-active {
  color: #409eff;
}

.setCon >>> .el-tabs__nav-scroll {
  background-color: #fff;
}
.setCon >>> .el-tabs__item {
  color: #303133;
}
.pageWrap {
  margin: 15px 10px 10px 0;
}
.avatar-uploader >>> .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader >>> .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

.avatar {
  width: 150px;
  height: 150px;
  display: block;
}

.tips {
  font-size: 12px;
  float: left;
  line-height: 1.2;
}

.avatar-uploader.banner >>> .el-upload {
  height: 123px !important;
}

.banner {
  width: 200px !important;
  height: 123px !important;
  line-height: 123px !important;
}
.bannerImgS {
  width: 200px;
  height: 124px;
}
</style>